package db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import config.DB;

public class DBConnection {
	public static String DRIVER;
	public static String JDBC_IP;
	public static String JDBC_PORT;
	public static String JDBC_DBNAME;
	public static String JDBC_CHARCODE;	
	public static String JDBC_URL;
	private static String JDBC_USER ;
    private static String JDBC_PASSWORD ;
    public static Connection conn;
    
	public DBConnection() throws SQLException
	{
		DB dbconfig = new DB();
		DRIVER = dbconfig.getDBDriver();
		JDBC_IP = dbconfig.getDBIp();
		JDBC_PORT = dbconfig.getDBPort();
		JDBC_DBNAME = dbconfig.getDBName();
		JDBC_CHARCODE = dbconfig.getDBCharcode();
		JDBC_USER = dbconfig.getDBUser();
		JDBC_PASSWORD = dbconfig.getDBUserCode();
		JDBC_URL="jdbc:mysql://"+JDBC_IP+":"+JDBC_PORT+"/"+JDBC_DBNAME+"?useUnicode=true&characterEncoding="+JDBC_CHARCODE;
	}
	
	public  Connection getConnection() throws SQLException {
	            try {
	                 Class.forName(DRIVER);
	                conn=DriverManager.getConnection(JDBC_URL,JDBC_USER,JDBC_PASSWORD);
	                
	                if(!conn.isClosed()) 
	                    System.out.println("Succeeded connecting to the Database!");
	                
	            } catch (Exception e) {
	                e.printStackTrace();
	            }
	            return conn;
	    }
	
	public void close(){
		if(conn!=null){
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	public static void close(Connection conn) {
        try {
            if (conn != null) {
                conn.close();
            }
        } catch (SQLException sqle) {
            sqle.printStackTrace();
        }
    }

    public static void close(PreparedStatement stmt) {
        try {
            if (stmt != null) {
                stmt.close();
            }
        } catch (SQLException sqle) {
            sqle.printStackTrace();
        }
    }
    public static void close(Statement stmt) {
        try {
            if (stmt != null) {
                stmt.close();
            }
        } catch (SQLException sqle) {
            sqle.printStackTrace();
        }
    }
    public static void close(ResultSet rs) {
        try {
            if (rs != null) {
                rs.close();
            }
        } catch (SQLException sqle) {
            sqle.printStackTrace();
        }

    }
    
    public int insertArtist(int id,String name,int follower,int following,int song_num,int album_num){
    	String insert_sql = "insert into artist values("+id+","+"\""+name+"\","+follower+","+following+","+song_num+","+album_num+")";
    	int state = 0;
    	try {
    		conn = getConnection();
			Statement st = conn.createStatement();
			state = st.executeUpdate(insert_sql);
			st.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	return state;
    }
    
    public int insertPeople(int id,String name,int follower,int following,int ting_age,int ting_level){
    	String insert_sql = "insert into people values("+id+","+"\""+name+"\","+follower+","+following+",NULL,"+ting_age+","+ting_level+",NULL)";
    	int state = 0;
    	try {
    		conn = getConnection();
			Statement st = conn.createStatement();
			state = st.executeUpdate(insert_sql);
			st.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	return state;
    }
}
